package com.swanclouds.redfish.account.domain;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.swanclouds.model.account.LdapGroup;
import com.swanclouds.model.account.builder.LdapGroupBuilder;
import com.swanclouds.redfish.common.redfish.MemberModel;

import java.util.List;
import java.util.stream.Collectors;

public class LdapGroupConcrete implements LdapGroup {

    @JsonProperty("MemberId")
    private Integer memberId;

    @JsonProperty("GroupName")
    private String groupName;

    @JsonProperty("GroupDomain")
    private String groupDomain;

    @JsonProperty("GroupFolder")
    private String groupFolder;

    @JsonProperty("GroupRole")
    private String groupRole;

    @JsonProperty("GroupLoginRule")
    private List<MemberModel> groupLoginRule;

    @JsonProperty("GroupLoginInterface")
    private List<String> groupLoginInterface;

    /**
     * 用户组id
     *
     * @return the group id
     */
    @Override
    public Integer getMemberId() {
        return this.memberId;
    }

    /**
     * 用户组名称
     *
     * @return the group name
     */
    @Override
    public String getGroupName() {
        return this.groupName;
    }

    /**
     * 用户组组域
     *
     * @return the group domain
     */
    @Override
    public String getGroupDomain() {
        return this.groupDomain;
    }

    @Override
    public String getGroupFolder() {
        return this.groupFolder;
    }

    /**
     * 用户组角色
     *
     * @return the group role
     */
    @Override
    public String getGroupRole() {
        return this.groupRole;
    }

    /**
     * 用户组登录规则，每一项为对象
     *
     * @return List odata.id具体登录规则的路径
     */
    @Override
    public List<String> getGroupLoginRule() {
        if (this.groupLoginRule == null) {
            return Lists.newArrayList();
        }
        return this.groupLoginRule.stream().map(MemberModel::getDataId).collect(Collectors.toList());
    }

    /**
     * 登录接口，每一项为字符串
     *
     * @return the group login interface
     */
    @Override
    public List<String> getGroupLoginInterface() {
        return this.groupLoginInterface;
    }


    @Override
    public LdapGroupBuilder toBuilder() {
        return new LdapGroupConcreteBuilder();
    }

    public static LdapGroupBuilder builder() {
        return new LdapGroupConcrete().toBuilder();
    }

    public static class LdapGroupConcreteBuilder implements LdapGroupBuilder {

        private LdapGroupConcrete m ;

        public LdapGroupConcreteBuilder () {
            this(new LdapGroupConcrete());
        }

        public LdapGroupConcreteBuilder(LdapGroupConcrete m) {
            this.m = m;
        }

        /**
         * Creates and return the Model Entity M
         *
         * @return M instance
         */
        @Override
        public LdapGroup build() {
            return m;
        }

        /**
         * Creates a Builder using the param M as the default values
         *
         * @param in The Model M
         * @return Builder
         */
        @Override
        public LdapGroupBuilder from(LdapGroup in) {
            m = (LdapGroupConcrete) in;
            return this;
        }

        @Override
        public LdapGroupBuilder GroupName(String groupName) {
            m.groupName = groupName;
            return this;
        }

        @Override
        public LdapGroupBuilder GroupFolder(String groupFolder) {
            m.groupFolder = groupFolder;
            return this;
        }

        @Override
        public LdapGroupBuilder GroupDomain(String groupDomain) {
            m.groupDomain = groupDomain;
            return this;
        }

        @Override
        public LdapGroupBuilder GroupRole(String groupRole) {
            m.groupRole = groupRole;
            return this;
        }

        /**
         * Ldap用户组的登录规则 数组，数组中是字符串，可填多个，形如：["Rule1","Rule2","Rule3"]
         *
         * @param groupLoginRule 登录规则
         * @return the builder ldap group
         */
        @Override
        public LdapGroupBuilder GroupLoginRule(List<String> groupLoginRule) {
            if (m.groupLoginRule == null) {
                m.groupLoginRule = Lists.newArrayList();
            }
            if (groupLoginRule == null) {
                return this;
            }
            groupLoginRule.stream().forEach(id -> {
                m.groupLoginRule.add(new MemberModel());
            });
            return this;
        }

        /**
         * Ldap用户组的登录接口数组，数组中是字符串，可填多个
         *
         * @param groupLoginInterface 登录接口
         * @return the builder ldap group
         */
        @Override
        public LdapGroupBuilder GroupLoginInterface(List<String> groupLoginInterface) {
            m.groupLoginInterface = groupLoginInterface;
            return this;
        }

        @Override
        public String toString() {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                return objectMapper.writeValueAsString(this);
            } catch (JsonProcessingException e) {
            }
            return "LdapGroupConcreteBuilder {" +
                    "m=" + m +
                    '}';
        }
    }

    @Override
    public String toString() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
        }
        return "LdapGroupConcrete {" +
                "memberId=" + memberId +
                ", groupName='" + groupName + '\'' +
                ", groupDomain='" + groupDomain + '\'' +
                ", groupFolder='" + groupFolder + '\'' +
                ", groupRole='" + groupRole + '\'' +
                ", groupLoginRule=" + groupLoginRule +
                ", groupLoginInterface=" + groupLoginInterface +
                '}';
    }
}